
#include <iostream>
#include <thread>
#include <future>

void Add(int num1, int num2, std::promise<int>& prom)
{
    std::cout << std::this_thread::get_id() << std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(3));
    std::cout << "3333"<< std::endl;
    prom.set_value(num1 + num2);
}

int main()
{
    std::cout << std::this_thread::get_id() << std::endl;
    //通过在线程中对promise对象设置数据，其他线程中通过future获取设置数据
    //的方式来实现获取异步任务执行结果的功能
    std::promise<int> prom;
    std::future<int> fu = prom.get_future();

    std::thread t(Add, 11, 22, std::ref(prom));
    std::cout << "1111"<< std::endl;
    std::this_thread::sleep_for(std::chrono::seconds(6));
    std::cout << "2222"<< std::endl;
    int ret = fu.get();
    std::cout << "sum= " << ret << std::endl;
    t.join();
    return 0;
}